/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sasp_messages;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author mauriello
 */
public class REQMasterReply extends SASPReply {
    REQMasterTuple data;

    public REQMasterReply( ) {
        super( );
        this.setType(REQ_SN);
    }

    public void setData( REQMasterTuple _data ) {
        data = _data;
    }

    public REQMasterTuple getData( ) {
        return data;
    }

    public REQMasterReply( int _o, int _d, int _r, REQMasterTuple _data ) {
        super( _o, _d, _r );
        data = _data;
        this.setType(REQ_SN);
        try {
            JSONObject body = new JSONObject();
            body.put("SNAlias", data.getSNAlias());
            body.put("IPAddress", data.getIPAddress());
            body.put("GroupType", data.getGroupType());
            body.put("GroupName", data.getGroupName());
            body.put("NumRetries", data.getNumRetries());
            body.put("MessDelay", data.getMessDelay());
            this.setReplyBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQMasterReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public REQMasterReply( String _s ) {
        super( _s );

        data = new REQMasterTuple();
        try {
            saspmanager.SASPManagerApp.addLine("RMR - Request Master Reply Received");
            data.setSNAlias(this.getReplyBody().getString("SNAlias"));
            data.setNumRetries(this.getReplyBody().getInt("NumRetries"));
            data.setMessDelay(this.getReplyBody().getInt("MessDelay"));
            data.setIPAddress(this.getReplyBody().getString("IPAddress"));
            data.setGroupType(this.getReplyBody().getString("GroupType"));
            data.setGroupName(this.getReplyBody().getString("GroupName"));
        } catch (JSONException ex) {
            Logger.getLogger(REQMasterReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void updateReplyBody() {
        try {
            JSONObject body = new JSONObject();
            body.put("SNAlias", data.getSNAlias());
            body.put("IPAddress", data.getIPAddress());
            body.put("GroupType", data.getGroupType());
            body.put("GroupName", data.getGroupName());
            body.put("NumRetries", data.getNumRetries());
            body.put("MessDelay", data.getMessDelay());
            this.setReplyBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQMasterReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
